﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for DA_Client
/// </summary>
public class DA_Client
{
    AuditBillingEntities objABEnt;
    public DA_Client()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public List<PLClient> DA_fnGetClientList(PLClient pObjClient)
    {
        using (objABEnt = new AuditBillingEntities())
        {
            var varRes = (from u in objABEnt.MstClients
                          where (pObjClient.SearchIsActive == null || u.IsActive == pObjClient.SearchIsActive) &&
                                (pObjClient.ClientID == null || pObjClient.ClientID == u.MstClientID) &&
                                (string.IsNullOrEmpty(pObjClient.ClientName) || u.ClientName.ToLower().Contains(pObjClient.ClientName.ToLower()))
                          orderby u.ClientName
                          select new PLClient()
                          {
                              ClientID = u.MstClientID,
                              ClientName = u.ClientName,
                              Industry = u.Industry,
                              Address1 = u.Address1,
                              Address2 = u.Address2,
                              City = u.City,
                              PinCode = u.PinCode,
                              Phone = u.Phone,
                              Email = u.Email,
                              Fax = u.Fax,
                              ContactPersonName = u.ContactPersonName,
                              ContactPersonPhone = u.ContactPersonPhone,
                              ContactPersonEmail = u.ContactPersonEmail,
                              PAN = u.PAN,
                              TIN = u.TIN,
                              IsServiceTaxAllowed = u.IsServiceTaxAllowed,
                              IsActive = u.IsActive,

                          });
            return varRes.ToList();
        }
    }

    public List<PLClient> DA_fnGetClientNameList(PLClient pObjClient)
    {
        using (objABEnt = new AuditBillingEntities())
        {
            var varRes = (from u in objABEnt.MstClients
                          where (pObjClient.SearchIsActive == null ? true : (pObjClient.ClientID == pObjClient.AddClientID || u.IsActive == pObjClient.SearchIsActive)) &&
                                (pObjClient.ClientID == null || pObjClient.ClientID == u.MstClientID)
                          orderby u.ClientName
                          select new PLClient()
                          {
                              ClientID = u.MstClientID,
                              ClientName = u.ClientName,
                              Email = u.Email,
                              ContactPersonName = u.ContactPersonName,
                              ContactPersonEmail = u.ContactPersonEmail,
                              IsActive = u.IsActive,
                          });
            return varRes.ToList();
        }
    }

    public int DA_InsertUpdateClient(PLClient pObjClient)
    {
        using (objABEnt = new AuditBillingEntities())
        {
            MstClient objClient = new MstClient();
            if (pObjClient.ClientID.GetValueOrDefault() != 0)
            {
                objClient = (from u in objABEnt.MstClients
                             where u.MstClientID == pObjClient.ClientID
                           select u).ToList().FirstOrDefault();
            }

            objClient.ClientName = pObjClient.ClientName;
            objClient.Industry = pObjClient.Industry;
            objClient.Address1 = pObjClient.Address1;
            objClient.Address2 = pObjClient.Address2;
            objClient.City = pObjClient.City;
            objClient.PinCode = pObjClient.PinCode;
            objClient.Phone = pObjClient.Phone;
            objClient.Email = pObjClient.Email;
            objClient.Fax = pObjClient.Fax;
            objClient.ContactPersonName = pObjClient.ContactPersonName;
            objClient.ContactPersonPhone = pObjClient.ContactPersonPhone;
            objClient.ContactPersonEmail = pObjClient.ContactPersonEmail;
            objClient.PAN = pObjClient.PAN;
            objClient.TIN = pObjClient.TIN;
            objClient.IsServiceTaxAllowed = pObjClient.IsServiceTaxAllowed;
            objClient.IsActive = pObjClient.IsActive;

            
            if (pObjClient.ClientID.GetValueOrDefault() != 0)
            {
                objClient.ModifiedBy = SessionInfo.prop_getObj.EmployeeID;
                objClient.ModifiedOn = DateTime.Now;
            }
            else
            {
                objClient.CreatedBy = SessionInfo.prop_getObj.EmployeeID;
                objClient.CreatedOn = DateTime.Now;
                objABEnt.MstClients.Add(objClient);
            }
            objABEnt.SaveChanges();
            return objClient.MstClientID;
        }
    } 
}